花花 16:36:15
<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="dAdd"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template v-slot="scope">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary"  @click="hEdit(scope.row)">编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="hDel(scope.row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <!-- 分页
              @current-change : 用户点击页码
              total: 总条数
              page-size : 每页几条
              current-page : 当前的页码
               
               -->
              <el-pagination
                background
                layout="prev, pager, next"
                :total="total"
                :page-size="2"
                @current-change="hCurrentChange"
                :current-page.sync="pageParams.page"
              >
              </el-pagination>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <!-- 新增弹框 -->
    <el-dialog
      :title="isEdit? '编辑': '新增'"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <el-form label-width="100px">
        <el-form-item label="角色名称" prop="name" >
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input  v-model="roleForm.description"  />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="showDialog=false">取消</el-button>
          <el-button size="small" type="primary" @click="hSubmit"
            >确定</el-button
          >
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>


<script>
import { getRoles, deleteRole,addRole,editRole} from "@/api/settings";
export default {
  data() {
    return {
      pageParams: {
        // 当前的页码
        page: 1,
        // 每页条数
        pagesize: 2,
      },
      // 后台返回的数据
      list: [],
      // 总条数
      total: 0,
      // 弹框
      showDialog: false,
      // 给后台接口的数据
      roleForm:{
        name:'',
        description:''
      },
      isEdit : false
    };
  },
  created() {
    this.loadRoles();
  },
  methods: {
    async loadRoles() {
      const res = await getRoles(this.pageParams);
      console.log(res);
      this.list = res.data.rows;
      this.total = res.data.total;
    },
    hCurrentChange(curPage) {
      this.pageParams.page = curPage;
      console.log(curPage);
      this.pageParams.page;
      this.loadRoles();
    },
    async doDel(id) {
      try {
        const res = await deleteRole(id);
        console.log(res);
        // 1. 提示
        this.$message.success("删除成功");
        // 2. 重新请求
      } catch (err) {
        console.log(err);
        this.$message.error("删除失败");
      }
    },
    // 删除
    hDel(id) {
      this.$confirm("你确定要删除吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.doDel(id);
          // 如果删除页面最后一条数据,页码要减一
          if (this.list.length === 1) {
            this.pageParams.page--;
            if (this.pageParams.page === 0) {
              this.pageParams.page = 1;
            }
          }
          this.loadRoles();
        })
        .catch(() => {
          // 用户取消退出
        });
    },
    // 新增按钮
    dAdd() {
      this.showDialog = true;
      this.isEdit = false
    },
    // 新增确定操作
    async doAdd(){

      await addRole(this.roleForm)
      this.loadRoles();
      this.hClose()
      // 让用户立刻看到数据
      // 查询最后一页的数据
      this.$message.success('新增成功')
      this.total++
      this.pageParams.page =Math.ceil(this.total / this.pageParams.pagesize)
      this.showDialog=false
    },
    hSubmit(){
      if(this.isEdit){
        this.doEdit()
      }else{
        // 新增确认
        this.doAdd()
      }
    },
    // 编辑
    hEdit(row){
      this.roleForm = {...row}
      this.showDialog = true;
      this.isEdit = true
    },
    //确认编辑
    async doEdit(){
      await editRole(this.roleForm)
      // 刷新页面
      this.$message.success('编辑成功')
      this.loadRoles();
      this.showDialog=false
      this.hClose()
    },
    // 取消
    hClose(){
      this.roleForm = { 
      name: '',
      description: ''
    }
    }

  },
};
</script>
 